import Vue from "vue"
import VueRouter from "vue-router"
import useStore from "@/store/index.js"
import { getToken } from "../utils/auth"
import PermissionsRouter from "@/router/PermissionsRouter.js"
import { MessageBox } from "element-ui"

Vue.use(VueRouter)

// 静态路由（默认路由），无需权限访问
const routes = [
	{
		path: "/",
		redirect: "/login",
	},
	{
		path: "/login",
		name: "Login",
		component: (resolve) => require(["../views/admin/Login"], resolve),
		meta: { title: "DemoXY快速开发框架" },
	},
	{
		path: "/userInfo",
		name: "userInfo",
		component: (resolve) => require(["../views/admin/userInfo"], resolve),
		meta: { title: "DemoXY快速开发框架-个人信息" },
	},

	{
		path: "/index",
		name: "index",
		meta: {
			title: "DemoXY快速开发框架",
			requireAuth: true,
		},
		component: (resolve) => require(["../views/admin/index"], resolve),
		children: [
			// 		{
			// 			path: "/dataSource",
			// 			component: (resolve) => require(["@/views/admin/gencode/dataSource"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-数据源管理", requireAuth: true },
			// 		},
			// 		{
			// 			path: "/codeGen",
			// 			component: (resolve) => require(["@/views/admin/gencode/codeGen"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-代码生成", requireAuth: true },
			// 		},
			// 		{
			// 			path: "/apiDocument",
			// 			component: (resolve) => require(["@/views/admin/apiDocument"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-API文档", requireAuth: true },
			// 		},
			// 		{
			// 			path: "/job",
			// 			component: (resolve) => require(["@/views/admin/job"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-定时任务", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/operationLog",
			// 			component: (resolve) => require(["@/views/admin/system/log/operationLog"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-操作日志", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/loginLog",
			// 			component: (resolve) => require(["@/views/admin/system/log/loginLog"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-登录日志", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/user",
			// 			component: (resolve) => require(["@/views/admin/permissions/user"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-用户管理", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/role",
			// 			component: (resolve) => require(["../views/admin/permissions/role"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-角色管理", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/menu",
			// 			component: (resolve) => require(["@/views/admin/permissions/menu"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-菜单管理", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/tenant",
			// 			component: (resolve) => require(["@/views/admin/permissions/tenant"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-租户管理", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/file",
			// 			component: (resolve) => require(["@/views/admin/system/file"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-文件管理", requireAuth: false },
			// 		},
			// 		{
			// 			path: "/server",
			// 			component: (resolve) => require(["@/views/admin/system/server"], resolve),
			// 			meta: { title: "DemoXY快速开发框架-服务监控", requireAuth: false },
			// 		},
		],
	},
]

const router = new VueRouter({
	routes,
})
let isRe = false

router.beforeEach((to, from, next) => {
	if (to.matched.some((res) => res.meta.requireAuth)) {
		// 验证是否需要登陆
		if (getToken()) {
			// 查询本地存储信息是否已经登陆
			next()
		} else {
			MessageBox.confirm("登录状态已过期,请重新登录", {
				confirmButtonText: "登录",
				cancelButtonText: "取消",
				type: "warning",
			})
				.then(() => {
					this.$router.push({ name: "Login" })
				})
				.catch(() => {})
			// next({
			// 	path: "/login", // 未登录则跳转至login页面
			// 	query: { redirect: to.fullPath }, // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
			// })
		}
	} else if (from.name === null) {
		if (isRe) {
			next()
		} else {
			setTimeout(() => {
				const store = useStore()
				if (Object.keys(store.router).length) {
					const routers = PermissionsRouter.createRoute(store.router)

					router.addRoute(routers)
					isRe = true
					next({
						...to,
						replace: true,
					})
				}
				next()
			})
		}
	} else {
		next()
	}
})

function toLogin() {}
export default router
